﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion


#region Usings

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Threading;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.UI.View.Util;
using ScrumTable.UI.View.WinTouch.UserControls.Tool;
using ScrumTable.UI.View.WinTouch.Util;
using ScrumTable.UI.ViewModel;
using ScrumTable.UI.ViewModel.Common;

#endregion

namespace ScrumTable.UI.View.WinTouch.UserControls
{
    /// <summary>
    /// Interaction logic for TaskControlBack.xaml
    /// </summary>
    public partial class TaskControlBack 
    {
        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------
        /// <summary>
        /// Initializes a new instance of the <see cref="TaskControlBack"/> class.
        /// </summary>
        public TaskControlBack()
        {
            InitializeComponent();

            string customGui = GuiLoader.GetCustomGuiFor("TaskControlBack");
            if (customGui != null)
            {
                FrameworkElement propertyElement = (FrameworkElement) XamlReader.Parse(customGui);
                propertyElement.Measure(new Size(double.MaxValue, double.MaxValue));
                AttributRow.Height = new GridLength(propertyElement.DesiredSize.Height);
                PropertyPart.Content = propertyElement;
            }
            Loaded += OnLoaded;
            DataContextChanged += OnDataContextChanged;
        }

        #endregion



        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
            var viewModel = (DataContext as TaskViewModel);
            if (viewModel != null && !viewModel.IsDisposed)
            {
                viewModel.Original.Save();
            }
            base.ReleaseManagedResources();
        }

        private void ShowPropertyPanel(DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null && e.OldValue != null && e.NewValue != e.OldValue)
            {
                PropertyPart.Content = null;
                MemoPart.Content = null;
            }
            if (PropertyPart.Content == null)
            {
                PropertyPart.Content = CreateDynamicUiFactory.CreateDynamicPart(DataContext as TaskViewModel, ElementManager.ElementConfig.TaskElement);
                MemoPart.Content = CreateDynamicUiFactory.CreateMemoParts(DataContext as TaskViewModel, ElementManager.ElementConfig.TaskElement);
            }
        }

        #endregion


        #region Events

        private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            ShowPropertyPanel(e);
        }

   
        private void OnLoaded(object sender, System.Windows.RoutedEventArgs e)
        {
            Title.SelectAll();
            Title.Focus();
            Loaded -= OnLoaded;
            
        }

        private void OnAddAttachmentClick(object sender, RoutedEventArgs e)
        {
            if (DataContext is TaskViewModel)
            {
                ScrumInkCanvas.CreateAndShowInkCanvas(DataContext as TaskViewModel);
            }
        }
       
        #endregion
    }
}
